<template>
	<view class="car-home-page">
		<view class="filter">
			<view class="tabs">
				<uv-tabs :list="tabList" lineColor="#FFC434" :scrollable="false" :inactiveStyle="inactiveStyle"
					:activeStyle="inactiveStyle" lineWidth="78rpx" lineHeight="8rpx" @click="tabChange"></uv-tabs>
			</view>
			<view class="pl" @click="handStatus">
				<template v-if="isEdit">
					退出管理
				</template>
				<template v-else>
					<image src="https://wuyeshop.oss-cn-beijing.aliyuncs.com/image/20230712/20230712103310_imdb.png"
						mode=""></image>
					<text>批量管理</text>
				</template>
			</view>
		</view>
		<common-list :isDataEmpty="tableData.length === 0" :status="status">
			<template #list>
				<view class="">
					<uv-checkbox-group v-model="checkboxValue" @change="checkChange">
						<view class="shopping-list">
							<view class="shopping-item" v-for="(item, index) in tableData" :key="index">
								<view class="checked">
									<uv-checkbox :disabled="item.status !== 'AUDIT_PASS'" shape="circle"
										active-color="#FF9113" :name="item.id"></uv-checkbox>
								</view>
								<view class="shopping-item-card" :style="{ marginLeft: isEdit ? '60rpx;' : '0' }">
									<template v-if="curren === 2">
										<view class="status" v-if="item.status === 'AUDIT'">
											审核中
										</view>
										<view class="status status-success" v-else-if="item.status === 'AUDIT_PASS'">
											待上架
										</view>
										<view class="status status-reject" v-else-if="item.status === 'AUDIT_REJECT'">
											驳回
										</view>
									</template>
									<car-card :isEdit="isEdit" isNoStock :info="item" @onClick="onClick(item)" />
									<view class="btns">
										<template v-if="item.status === 'AUDIT_PASS'">
											<button v-if="curren === 0" class="cu-btn"
												@click="handUpdStock(item)">改库存</button>
											<button class="cu-btn"
												@click="handPlUpDown(item)">{{ curren === 1 ? '下架'  : '上架'}}</button>
											<button v-if="curren === 0" class="cu-btn btn-edit"
												@click="handAdd(item, index)">编辑</button>
										</template>
										<template v-else-if="item.status === 'AUDIT_REJECT'">
											<button class="cu-btn" @click="handReason(item.reason)">查看原因</button>
											<button class="cu-btn btn-edit" @click="handAdd(item, index)">修改</button>
										</template>
									</view>
								</view>
							</view>
						</view>
					</uv-checkbox-group>
				</view>
			</template>
		</common-list>
		<view class="action flex align-center justify-between bg-white">
			<template v-if="isEdit">
				<view class="action-left">
					<uv-checkbox-group v-model="checkboxAll" @change="allChange">
						<uv-checkbox shape="circle" active-color="#FF9113" label="全选" name="all"></uv-checkbox>
					</uv-checkbox-group>
				</view>
				<view class="btns flex align-center">
					<button v-if="curren === 0" class="cu-btn del-btn" @click="handPlDel">批量删除</button>
					<button class="cu-btn sj-btn" @click="handPlUpDown()">
						{{ curren === 1 ? '批量下架'  : '批量上架'}}
					</button>
				</view>
			</template>
			<template v-else>
				<button class="cu-btn btn" @click="handAdd()">添加车辆</button>
			</template>
		</view>
		<upd-sort ref="stokRef" @change="stockChange" />
		<reasonPop ref="reasonRef" />
		<uv-modal ref="delRef" show-cancel-button confirm-color="#FF9113" @confirm="handConfirm" width="550rpx">
			<view style="line-height: 100rpx;">确认要删除吗?</view>
		</uv-modal>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		nextTick
	} from 'vue'
	import CommonList from '@/components/commonList/list.vue'
	import CarCard from '@/components/card/CarCard.vue'
	import UpdSort from './components/updSort.vue'
	import reasonPop from './components/reasonPop.vue'
	import {
		doList,
		doDetail,
		doUpdShelves,
		doDels
	} from '@/apis/car.js'
	import {
		userList
	} from '@/hooks/userList.js'

	import {
		onLoad,
		onReachBottom,
		onPullDownRefresh
	} from "@dcloudio/uni-app";

	const reasonRef = ref()
	const handReason = (e) => {
		reasonRef.value.handOpen(e)
	}

	const tabList = reactive([{
		name: '待上架'
	}, {
		name: '出租中',
	}, {
		name: '审核中',
	}])
	const checkboxValue = ref([])
	const checkboxAll = ref([])
	const allChange = (e) => {
		if (e.length === 1) {
			checkboxValue.value = uni.$uv.deepClone(tableData.value).filter(el => {
				return el.status === 'AUDIT_PASS'
			}).map(el => {
				return el.id
			})
		} else {
			checkboxValue.value = []
		}
	}
	const checkChange = () => {
		nextTick(() => {
			if (checkboxValue.value.length === tableData.value.filter(el => el.status === 'AUDIT_PASS')
				.length) {
				checkboxAll.value = ['all']
			} else {
				checkboxAll.value = []
			}
		})
	}
	const onClick = (e) => {
		const index = checkboxValue.value.findIndex(el => el === e.id)
		if (index >= 0) {
			checkboxValue.value.splice(index, 1)
		} else {
			checkboxValue.value.push(e.id)
		}
		checkChange()
	}
	const handPlUpDown = async (item) => {
		let ids = []
		if (item) {
			if (item.status !== 'AUDIT_PASS') {
				uni.$uv.toast('车辆未审核通过,请耐心等待')
				return
			}
			ids = [item.id]
		} else {
			if (checkboxValue.value.length === 0) {
				uni.$uv.toast('请至少选中一条数据')
				return
			}
			const isStatus = checkboxValue.value.some(el => {
				const info = tableData.value.find(item => item.id === el)
				return info.status !== 'AUDIT_PASS'
			})
			if (isStatus) {
				uni.$uv.toast('选中数据包含未审核通过车辆')
				return
			}
			ids = checkboxValue.value
		}
		await doUpdShelves({
			ids,
			isShelves: curren.value === 0
		})
		uni.$uv.toast(curren.value === 0 ? '上架成功' : '下架成功')
		isEdit.value = false
		checkboxAll.value = []
		checkboxValue.value = []
		if (item) {
			const index = tableData.value.findIndex(el => el.id === item.id)
			tableData.value.splice(index, 1)
		} else {
			onRefresh()
		}
	}

	const delRef = ref()
	const handPlDel = () => {
		if (checkboxValue.value.length === 0) {
			uni.$uv.toast('请至少选中一条数据')
			return
		}
		delRef.value.open()
	}
	const handConfirm = async () => {
		await doDels({
			ids: checkboxValue.value
		})
		uni.$uv.toast('删除成功')
		isEdit.value = false
		checkboxAll.value = []
		checkboxValue.value = []
		onRefresh()
	}

	const stokRef = ref(null)
	const handUpdStock = (item) => {
		stokRef.value.handOpen(uni.$uv.deepClone(item))
	}

	const stockChange = (e) => {
		const index = tableData.value.findIndex(el => el.id === e.id)
		tableData.value[index].stock = e.stock
	}

	let curren = ref(0)
	const tabChange = (e) => {
		curren.value = e.index
		if (e.index === 2) {
			queryForm.value.status = 'AUDIT'
			delete queryForm.value.isShelves
		} else {
			queryForm.value.isShelves = e.index === 1
			queryForm.value.status = 'AUDIT_PASS'
		}
		if (isEdit) {
			isEdit.value = false
			checkboxAll.value = []
			checkboxValue.value = []
		}
		onRefresh()
	}
	const inactiveStyle = reactive({
		color: '#333333',
		fontSize: '32rpx',
		fontWeight: '550'
	})

	const isEdit = ref(false)
	const handStatus = () => {
		isEdit.value = !isEdit.value
		if (!isEdit.value) {
			checkboxValue.value = []
			checkboxAll.value = []
		}
	}


	const handAdd = (row, index) => {
		if (row) {
			uni.navigateTo({
				url: `/pagesA/dealers/addCar?id=${row.id}`,
				events: {
					success: () => {
						rowChange(index)
					}
				}
			})
		} else {
			uni.navigateTo({
				url: '/pagesA/dealers/addCar',
				events: {
					success: () => {
						onRefresh()
					}
				}
			})
		}
	}

	const rowChange = async (index) => {
		const info = tableData.value[index]
		const data = await doDetail(info.id)
		if (curren.value === 0) {
			if (data.isShelves) {
				tableData.value.splice(index)
				return
			}
			if (data.status !== 'AUDIT_PASS') {
				tableData.value.splice(index)
				return
			}
		}
		if (data.platform) {
			data.platform = JSON.parse(data.platform)
		} else {
			data.platform = []
		}
		data.schemes = JSON.parse(data.schemes)
		data.strength = JSON.parse(data.strength)
		if (!data.detailsImg) {
			data.detailsImg = []
		}
		tableData.value[index] = data
	}

	/**
	 * 
	 * 列表数据
	 * */
	const getApi = async (params) => {
		const res = await doList(params)
		res.records = res.records.map(el => {
			if (el.platform) {
				el.platform = JSON.parse(el.platform)
			} else {
				el.platform = []
			}
			return el
		})
		return res
	}
	const {
		tableData,
		status,
		fetchData,
		onBottom,
		onRefresh,
		queryForm
	} = userList({
		apis: {
			getApi
		},
		initForm: {
			isShelves: false,
			status: 'AUDIT_PASS'
		}
	})

	onPullDownRefresh(() => {
		onRefresh()
	})
	onReachBottom(() => {
		onBottom()
	})

	onLoad(() => {
		onRefresh()
	})
</script>

<style lang="scss" scoped>
	.car-home-page {
		min-height: 100vh;
		background-color: #F5F5F5;

		.filter {
			position: sticky;
			top: 0;
			background-color: #fff;
			z-index: 3;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 0 24rpx;

			.tabs {
				width: 460rpx;
			}

			.pl {
				image {
					width: 25.15rpx;
					height: 25.15rpx;
				}

				text {
					margin-left: 6rpx;
					font-size: 26rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #333333;
				}
			}
		}

		.shopping-list {
			width: 702rpx;
			margin: 24rpx auto 0 auto;

			.shopping-item {
				position: relative;

				.checked {
					position: absolute;
					height: 100%;
					z-index: 1;
					display: flex;
					align-items: center;
				}

				.shopping-item-card {
					width: 702rpx;
					background-color: #fff;
					position: relative;
					z-index: 3;
					transition: all 0.2s;
					padding: 28rpx 24rpx;
					border-radius: 16rpx;

					.status {
						position: absolute;
						right: 0;
						top: 0;
						background-color: #ff9900;
						color: #fff;
						padding: 5rpx 15rpx 5rpx 20rpx;
						border-radius: 0 0 0 25rpx;

						&-reject {
							background-color: #fa3534;
						}

						&-success {
							background-color: #19be6b;
						}
					}


					.btns {
						display: flex;
						justify-content: flex-end;
						margin-top: 32rpx;

						.cu-btn {
							margin-left: 20rpx;
						}

						.btn {
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #333333;
							border-radius: 16rpx;
							background-color: #F5F5F5;
						}

						.btn-edit {
							font-size: 28rpx;
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #FF8800;
							border-radius: 16rpx;
							background: #FFF7E4;
						}
					}
				}

				&:not(:first-child) {
					margin-top: 20rpx;
				}
			}

		}

		.action {
			position: fixed;
			z-index: 5;
			width: 750rpx;
			bottom: 0;
			left: 0;
			background-color: #fff;
			padding: 16rpx 24rpx;
			padding-bottom: calc(constant(safe-area-inset-bottom) + 16rpx);
			padding-bottom: calc(env(safe-area-inset-bottom) + 16rpx);

			.btn {
				font-size: 32rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #333333;
				width: 100%;
				height: 88rpx;
				line-height: 88rpx;
				background: #FFC434;
				border-radius: 16rpx;
			}

			.btns {
				.cu-btn {
					font-size: 30rpx;
					height: 74rpx;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					padding: 16rpx 50rpx;
					margin-left: 20rpx;
				}

				.del-btn {
					background: #F5F5F5;
				}

				.sj-btn {
					background: #FFC434;
				}
			}
		}
	}
</style>